package test2constructor;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.GenericApplicationContext;

import javax.annotation.PostConstruct;

/**
 * @author: mornd
 * @dateTime: 2023/5/17 - 20:24
 * @Lazy 解决构造器产生的循环依赖
 */
@Slf4j
public class Demo {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("aaa", AAA.class);
        context.registerBean("bbb", BBB.class);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.refresh();
        AAA aaa = context.getBean(AAA.class);
        BBB bbb = context.getBean(BBB.class);
        System.out.println("-------------------------");
        System.out.println(aaa.getClass());
        System.out.println(bbb.getClass());
        System.out.println(bbb.aaa.getClass());
        bbb.aaa.foo();
    }

    static class AAA {
        public AAA( BBB bbb) {
            log.debug("AAA({})", bbb.getClass().getName());
        }

        public void foo() {
            System.out.println("foo.....");
        }
    }

    static class BBB {
        private AAA aaa;

        public BBB(@Lazy AAA aaa) {
            log.debug("BBB({})", aaa.getClass().getName());
            this.aaa = aaa;
        }
    }

}
